import unittest

from django.test import TestCase
from datetime import datetime

from backend.biz import SupplierBiz, BuyOrderBiz, DeliveryBiz, InvoiceBiz, PaymentBiz


class SupplierBizTest(TestCase):
    def setUp(self):
        self.supplier_biz = SupplierBiz()

    def test_crud(self):
        supplier = self.supplier_biz.create({
            'name': 'PDD',
            'contact_name': 'Huang zheng',
            'contact_no': '13812348765'
        })
        self.assertEqual(1, supplier['id'])
        self.assertEqual('PDD', supplier['name'])
        self.assertEqual('Huang zheng', supplier['contact_name'])

        suppliers = self.supplier_biz.list()
        self.assertEqual(1, len(suppliers))
        self.assertEqual(supplier, suppliers[0])

        supplier = self.supplier_biz.create({
            'name': 'JD',
            'contact_name': 'Liu qiangdong',
            'contact_no': '13812348777'
        })
        self.assertEqual(2, supplier['id'])
        self.assertEqual('JD', supplier['name'])
        self.assertEqual('Liu qiangdong', supplier['contact_name'])

        suppliers = self.supplier_biz.list()
        self.assertEqual(2, len(suppliers))
        self.assertEqual(supplier, suppliers[1])

        up_supplier = self.supplier_biz.update(supplier['id'], {'contact_name': 'Zhang zetian'})
        self.assertEqual('JD', up_supplier['name'])
        self.assertEqual('Zhang zetian', up_supplier['contact_name'])

        self.assertEqual(2, len(suppliers))
        self.supplier_biz.destroy(up_supplier['id'])
        suppliers = self.supplier_biz.list()
        self.assertEqual(1, len(suppliers))


class BuyOrderBizTest(TestCase):
    def setUp(self):
        self.supplier_biz = SupplierBiz()
        self.order_biz = BuyOrderBiz()

    def test_create_no_supplier(self):
        ret = self.order_biz.create({
            'buy_date_time': datetime.now(),
            'digest': 'flowers',
            'amount': 1,
            'price': 35,
            'unit': '束'
        })
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual('flowers', ret['digest'])
        self.assertEqual(None, ret['supplier_detail'])

    def test_create_with_supplier(self):
        supplier = self.supplier_biz.create({
            'name': 'PDD',
            'contact_name': 'Huang zheng',
            'contact_no': '13812348765'
        })
        ret = self.order_biz.create({
            'supplier': supplier['id'],
            'buy_date_time': '2024-04-16 16:37:59',
            'digest': 'flowers',
            'amount': 1,
            'price': 35,
            'unit': '束'
        })
        self.assertEqual('flowers', ret['digest'])
        self.assertEqual('PDD', ret['supplier_detail']['name'])

    def test_create_with_default_supplier(self):
        ret = self.order_biz.create({
            'supplier': 0,
            'buy_date_time': '2024-04-16 16:37:59',
            'digest': 'fruit',
            'amount': 1,
            'price': 350,
            'unit': '框'
        })
        self.assertEqual('fruit', ret['digest'])
        self.assertEqual('System default supplier', ret['supplier_detail']['name'])

        ret = self.order_biz.create({
            'supplier': 0,
            'buy_date_time': '2024-04-17 10:37:59',
            'digest': 'apple',
            'amount': 1,
            'price': 150,
            'unit': '框'
        })
        self.assertEqual('apple', ret['digest'])
        self.assertEqual('System default supplier', ret['supplier_detail']['name'])

    def test_create_with_supplier_not_exists(self):
        with self.assertRaises(ValueError):
            self.order_biz.create({
                'supplier': 1,
                'buy_date_time': '2024-04-16 16:37:59',
                'digest': 'fruit',
                'amount': 1,
                'price': 350,
                'unit': '框'
            })


class DeliveryBizTest(TestCase):
    def setUp(self):
        self.delivery_biz = DeliveryBiz()
        self.order_biz = BuyOrderBiz()

    def test_create_with_no_order(self):
        delivery = self.delivery_biz.create({
            'take_delivery_date_time': datetime.now(),
            'take_delivery_no': '2023041800000089'
        })

        self.assertEqual(1, delivery['id'])
        self.assertEqual(delivery['take_delivery_no'], '2023041800000089')
        self.assertEqual(delivery['buy_order_detail'], None)

    def test_create_with_order(self):
        order = self.order_biz.create({
            'buy_date_time': datetime.now(),
            'digest': 'fruit',
            'amount': 1,
            'price': 350,
            'unit': '框'
        })
        self.assertEqual(1, order['id'])
        self.assertEqual(0, order['is_take_delivery'])

        delivery = self.delivery_biz.create({
            'buy_order': order['id'],
            'take_delivery_date_time': datetime.now(),
            'take_delivery_no': '2023041800000119'
        })
        self.assertEqual(delivery['take_delivery_no'], '2023041800000119')
        self.assertNotEqual(delivery['buy_order_detail'], None)
        self.assertEqual(delivery['buy_order_detail']['digest'], 'fruit')
        self.assertEqual(1, delivery['buy_order_detail']['is_take_delivery'])


class InvoiceBizTest(TestCase):
    def setUp(self):
        self.invoice_biz = InvoiceBiz()
        self.order_biz = BuyOrderBiz()

    def test_create_with_no_order(self):
        invoice = self.invoice_biz.create({
            'recv_invoice_date_time': datetime.now(),
            'recv_invoice_no': '2023041800000089',
            'recv_invoice_file_url': './files/1002.pdf'
        })

        self.assertEqual(1, invoice['id'])
        self.assertEqual(invoice['recv_invoice_no'], '2023041800000089')
        self.assertEqual(invoice['buy_order_detail'], None)

    def test_create_with_order(self):
        order = self.order_biz.create({
            'buy_date_time': datetime.now(),
            'digest': 'fruit',
            'amount': 1,
            'price': 350,
            'unit': '框'
        })
        self.assertEqual(1, order['id'])
        self.assertEqual(0, order['is_recv_invoice'])

        invoice = self.invoice_biz.create({
            'buy_order': order['id'],
            'recv_invoice_date_time': datetime.now(),
            'recv_invoice_no': '2023041800000119',
            'recv_invoice_file_url': './files/119.pdf'
        })
        self.assertEqual(invoice['recv_invoice_no'], '2023041800000119')
        self.assertNotEqual(invoice['buy_order_detail'], None)
        self.assertEqual(invoice['buy_order_detail']['digest'], 'fruit')
        self.assertEqual(1, invoice['buy_order_detail']['is_recv_invoice'])


class PaymentBizTest(TestCase):
    def setUp(self):
        self.payment_biz = PaymentBiz()
        self.order_biz = BuyOrderBiz()

    def test_create_with_no_order(self):
        payment = self.payment_biz.create({
            'pay_date_time': datetime.now(),
            'pay_type': 1,
            'amount': 199.99,
            'pay_flow_seq': '202404180101010001'
        })

        self.assertEqual(1, payment['id'])
        self.assertEqual(payment['pay_flow_seq'], '202404180101010001')
        self.assertEqual(payment['buy_order_detail'], None)

    def test_create_with_order(self):
        order = self.order_biz.create({
            'buy_date_time': datetime.now(),
            'digest': 'fruit',
            'amount': 1,
            'price': 350,
            'unit': '框'
        })
        self.assertEqual(1, order['id'])
        self.assertEqual(0, order['is_payed'])

        payment = self.payment_biz.create({
            'buy_order': order['id'],
            'pay_date_time': datetime.now(),
            'pay_type': 1,
            'amount': 350,
            'pay_flow_seq': '202404180101010001'
        })
        self.assertEqual(payment['pay_flow_seq'], '202404180101010001')
        self.assertNotEqual(payment['buy_order_detail'], None)
        self.assertEqual(payment['buy_order_detail']['digest'], 'fruit')
        self.assertEqual(1, payment['buy_order_detail']['is_payed'])


if __name__ == '__main__':
    unittest.main(verbosity=2)
